import store from 'store'
function logout() {
	store.commit('logout');
}
function askLogin() {
	if (store.state.loginPopup) {
		return
	}
	store.commit("setLoginPopup", true)
	uni.showModal({
		title: "您还没有登陆，请先登陆！",
		cancelText: "再看看",
		confirmText: "去登陆",
		success: (res) => {
			store.commit("setLoginPopup", false)
			if (res.confirm) {
				store.commit("navToLogin", 500);
			} else {
				store.commit("finishLogin");
			}
		}
	})
}

const db = uniCloud.database()
const dbCmd = db.command

module.exports = {
	db: db,
	cmd: dbCmd,
	removeById: async (collectionName, id, {showLoading = true}) => {
		return new Promise((resolve, reject) => {
			if (showLoading) {
				uni.showLoading({
					mask: true
				})
			}
			db.collection(collectionName).doc(id).remove()
				.then(res => {
					if (!res.result.code) {
						resolve()
					} else if (res.result.code === 'TOKEN_INVALID' || res.result.code === 'TOKEN_INVALID_TOKEN_EXPIRED' || res.result.code === 'TOKEN_INVALID_ANONYMOUS_USER') {
						logout();
						askLogin();
					} else {
						uni.showToast({
							title: res.result.message || '服务器有些累了，请稍后重试！',
							icon: 'none',
							duration: 1500
						})
						reject(res.result.message)
					}
					if (showLoading) {
						uni.hideLoading()
					}
				})
				.catch(err => {
					if (showLoading) {
						uni.hideLoading()
					}
					if (err.message && /(30205|30203)/.test(err.message)) {
						logout();
						askLogin();
					} else {
						uni.showToast({
							title: err.message || '网络忙，请重试！',
							icon: 'none',
							duration: 1500
						})
					}
					reject(err)
				})
		})
	},
	insert: async (collectionName, {showLoading = true, data}) => {
		return new Promise((resolve, reject) => {
			if (showLoading) {
				uni.showLoading({
					mask: true
				})
			}
			db.collection(collectionName).add(data)
				.then(res => {
					if (!res.result.code) {
						data._id = res.result.id
						resolve(data)
					} else if (res.result.code === 'TOKEN_INVALID' || res.result.code === 'TOKEN_INVALID_TOKEN_EXPIRED' || res.result.code === 'TOKEN_INVALID_ANONYMOUS_USER') {
						logout();
						askLogin();
					} else {
						uni.showToast({
							title: res.result.message || '服务器有些累了，请稍后重试！',
							icon: 'none',
							duration: 1500
						})
						reject(res.result.message)
					}
					if (showLoading) {
						uni.hideLoading()
					}
				})
				.catch(err => {
					if (showLoading) {
						uni.hideLoading()
					}
					if (err.message && /(30205|30203)/.test(err.message)) {
						logout();
						askLogin();
					} else {
						uni.showToast({
							title: err.message || '网络忙，请重试！',
							icon: 'none',
							duration: 1500
						})
					}
					reject(err)
				})
		})
	},
	update: async (collectionName, id, {showLoading = true, data}) => {
		return new Promise((resolve, reject) => {
			if (showLoading) {
				uni.showLoading({
					mask: true
				})
			}
			db.collection(collectionName).doc(id).update(data)
				.then(res => {
					if (!res.result.code) {
						resolve()
					} else if (res.result.code === 'TOKEN_INVALID' || res.result.code === 'TOKEN_INVALID_TOKEN_EXPIRED' || res.result.code === 'TOKEN_INVALID_ANONYMOUS_USER') {
						logout();
						askLogin();
					} else {
						uni.showToast({
							title: res.result.message || '服务器有些累了，请稍后重试！',
							icon: 'none',
							duration: 1500
						})
						reject(res.result.message)
					}
					if (showLoading) {
						uni.hideLoading()
					}
				})
				.catch(err => {
					if (showLoading) {
						uni.hideLoading()
					}
					if (err.message && /(30205|30203)/.test(err.message)) {
						logout();
						askLogin();
					} else {
						uni.showToast({
							title: err.message || '网络忙，请重试！',
							icon: 'none',
							duration: 1500
						})
					}
					reject(err)
				})
		})
	},
	getById: async (collectionName, id, {showLoading = true, field}) => {
		return new Promise((resolve, reject) => {
			if (showLoading) {
				uni.showLoading({
					mask: true
				})
			}
			var collection = db.collection(collectionName).doc(id)
			if (field) {
				collection = collection.field(field)
			}
			collection.get()
				.then(res => {
					// console.log('getById', res)
					if (!res.result.code) {
						resolve(res.result.data[0])
					} else if (res.result.code === 'TOKEN_INVALID' || res.result.code === 'TOKEN_INVALID_TOKEN_EXPIRED' || res.result.code === 'TOKEN_INVALID_ANONYMOUS_USER') {
						logout();
						askLogin();
					} else {
						uni.showToast({
							title: res.result.message || '服务器有些累了，请稍后重试！',
							icon: 'none',
							duration: 1500
						})
						reject(res.result.message)
					}
					if (showLoading) {
						uni.hideLoading()
					}
				})
				.catch(err => {
					// console.error('getById', err)
					if (showLoading) {
						uni.hideLoading()
					}
					if (err.message && /(30205|30203)/.test(err.message)) {
						logout();
						askLogin();
					} else {
						uni.showToast({
							title: err.message || '网络忙，请重试！',
							icon: 'none',
							duration: 1500
						})
					}
					reject(err)
				})
		})
	},
	getOne: async (collectionName, {showLoading = true, field, where, orderBy}) => {
		return new Promise((resolve, reject) => {
			if (showLoading) {
				uni.showLoading({
					mask: true
				})
			}
			var collection = db.collection(collectionName)
			if (where) {
				collection = collection.where(where)
			}
			if (field) {
				collection = collection.field(field)
			}
			if (orderBy) {
				collection = collection.orderBy(orderBy)
			}
			collection.get()
				.then(res => {
					if (!res.result.code) {
						if (res.result.data.length > 1) {
							reject('存在多条数据')
						} else {
							resolve(res.result.data[0])
						}
					} else if (res.result.code === 'TOKEN_INVALID' || res.result.code === 'TOKEN_INVALID_TOKEN_EXPIRED' || res.result.code === 'TOKEN_INVALID_ANONYMOUS_USER') {
						logout();
						askLogin();
					} else {
						uni.showToast({
							title: res.result.message || '服务器有些累了，请稍后重试！',
							icon: 'none',
							duration: 1500
						})
						reject(res.result.message)
					}
					if (showLoading) {
						uni.hideLoading()
					}
				})
				.catch(err => {
					if (showLoading) {
						uni.hideLoading()
					}
					if (err.message && /(30205|30203)/.test(err.message)) {
						logout();
						askLogin();
					} else {
						uni.showToast({
							title: err.message || '网络忙，请重试！',
							icon: 'none',
							duration: 1500
						})
					}
					reject(err)
				})
		})
	},
	getCount: async (collectionName, {showLoading = true, where}) => {
		return new Promise((resolve, reject) => {
			if (showLoading) {
				uni.showLoading({
					mask: true
				})
			}
			var collection = db.collection(collectionName)
			if (where) {
				collection = collection.where(where)
			}
			collection.count()
				.then(res => {
					if (!res.result.code) {
						resolve(res.result.total)
					} else if (res.result.code === 'TOKEN_INVALID' || res.result.code === 'TOKEN_INVALID_TOKEN_EXPIRED' || res.result.code === 'TOKEN_INVALID_ANONYMOUS_USER') {
						logout();
						askLogin();
					} else {
						uni.showToast({
							title: res.result.message || '服务器有些累了，请稍后重试！',
							icon: 'none',
							duration: 1500
						})
						reject(res.result.message)
					}
					if (showLoading) {
						uni.hideLoading()
					}
				})
				.catch(err => {
					if (showLoading) {
						uni.hideLoading()
					}
					if (err.message && /(30205|30203)/.test(err.message)) {
						logout();
						askLogin();
					} else {
						uni.showToast({
							title: err.message || '网络忙，请重试！',
							icon: 'none',
							duration: 1500
						})
					}
					reject(err)
				})
		})
	},
	getList: async (collectionName, {showLoading = true, where, field, orderBy}) => {
		return new Promise((resolve, reject) => {
			if (showLoading) {
				uni.showLoading({
					mask: true
				})
			}
			var collection = db.collection(collectionName)
			if (where) {
				collection = collection.where(where)
			}
			if (field) {
				collection = collection.field(field)
			}
			if (orderBy) {
				collection = collection.orderBy(orderBy)
			}
			collection.get()
				.then(res => {
					// console.log('getList', res)
					if (!res.result.code) {
						store.commit("finishLogin");
						resolve(res.result.data)
					} else if (res.result.code === 'TOKEN_INVALID' || res.result.code === 'TOKEN_INVALID_TOKEN_EXPIRED' || res.result.code === 'TOKEN_INVALID_ANONYMOUS_USER') {
						logout();
						askLogin();
					} else {
						uni.showToast({
							title: res.result.message || '服务器有些累了，请稍后重试！',
							icon: 'none',
							duration: 1500
						})
						reject(res.result.message)
					}
					if (showLoading) {
						uni.hideLoading()
					}
				})
				.catch(err => {
					if (err.message && /(30205|30203)/.test(err.message)) {
						logout();
						askLogin();
					} else {
						uni.showToast({
							title: err.message || '网络忙，请重试！',
							icon: 'none',
							duration: 1500
						})
					}
					reject(err)
				})
		})
	},
	getPage: async (collectionName, {showLoading = true, where, field, orderBy, pageNum = 1, pageSize = 20}) => {
		return new Promise((resolve, reject) => {
			if (showLoading) {
				uni.showLoading({
					mask: true
				})
			}
			let result = {
				total: 0,
				list: []
			}
			var collection = db.collection(collectionName)
			if (where) {
				collection = collection.where(where)
			}
			collection.count()
				.then(res => {
					if (!res.result.code) {
						result.total = res.result.total
						if (!result.total) {
							resolve(result)
							if (showLoading) {
								uni.hideLoading()
							}
						} else {
							collection = db.collection(collectionName)
							if (where) {
								collection = collection.where(where)
							}
							if (field) {
								collection = collection.field(field)
							}
							if (orderBy) {
								collection = collection.orderBy(orderBy)
							}
							collection = collection.skip((pageNum - 1) * pageSize)
							collection = collection.limit(pageSize)
							return collection.get()
						}
					} else if (res.result.code === 'TOKEN_INVALID' || res.result.code === 'TOKEN_INVALID_TOKEN_EXPIRED' || res.result.code === 'TOKEN_INVALID_ANONYMOUS_USER') {
						logout();
						askLogin();
					} else {
						uni.showToast({
							title: res.result.message || '服务器有些累了，请稍后重试！',
							icon: 'none',
							duration: 1500
						})
						reject(res.result.message)
					}
				})
				.then(res => {
					if (res) {
						if (!res.result.code) {
							result.list = res.result.data
							resolve(result)
						} else {
							uni.showToast({
								title: res.result.message || '服务器有些累了，请稍后重试！',
								icon: 'none',
								duration: 1500
							})
							reject(res.result.message)
						}
						if (showLoading) {
							uni.hideLoading()
						}
					}
				})
				.catch(err => {
					if (showLoading) {
						uni.hideLoading()
					}
					if (err.message && /(30205|30203)/.test(err.message)) {
						logout();
						askLogin();
					} else {
						uni.showToast({
							title: err.message || '网络忙，请重试！',
							icon: 'none',
							duration: 1500
						})
					}
					reject(err)
				})
		})
	}
}